//
//  UIImage+Extension.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/16.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

extension UIImage{
    
    
    
    func scaleToWidth(width:CGFloat) -> (UIImage){
        
        
        if self.size.width <= 600{
            return self
        }
        
        //原来的比例 1800 * 900
        //现在的比例 600 * ?
        
        let height = width / self.size.width * self.size.height
        
        let rect = CGRect(origin: CGPointZero, size: CGSize(width: width, height: height))
        
        //1.开启图形上下文
        UIGraphicsBeginImageContext(rect.size)
        //2.将当前图片画到Image上
        self.drawInRect(rect)
        //3.取到图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //4.关闭图形上下文
        UIGraphicsEndImageContext()
        //5.返回图片
        return image
    }
}
